package m.jclecai.com.fragment.goldManager;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.Response.ResponseGoldDetailList;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

/**
 * Created by andy on 2017/7/4.
 */

public class GoldDetailFragment extends BaseFragment  implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.rv_gold_detail)
    RecyclerView mRvGoldDetail;
    @BindView(R.id.rl_refresh_layout)
    AutoSwipeRefreshLayout mRlRefreshLayout;

    private MyAdapter mMyAdapter;
    private UserBean mUserBean;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_gold_detail;
    }

    @Override
    public void initData() {
        initUser();
        mRlRefreshLayout.setOnRefreshListener(this);
        mRlRefreshLayout.setRefreshing(true);
        requestGoldDetail();
    }

    private void initUser() {
        mUserBean = PreferenceUtil.readUser(getActivity());
    }


    private void requestGoldDetail() {
        //这里我们是需要获取数据的；
        //请求网络获取金币；

        if (mUserBean == null) {
            DisPlayContent("请先登录！！！");
            return;
        }
        String userName = PreferenceUtil.getUserName(getActivity());
        String password = PreferenceUtil.getPassword(getActivity());
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);
        pair.put("password", password);

        HttpManager.postAsyn(Constant_Url.GOLD_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {

                if (mRlRefreshLayout != null)
                    mRlRefreshLayout.setRefreshing(false);
                DisPlayContent("请检查网络！！！");
            }

            @Override
            public void onResponse(String response) {
               if (mRlRefreshLayout != null){
                   mRlRefreshLayout.setRefreshing(false);
               }
                try {
                    ResponseGoldDetailList responseSignList = FastOk.get(response, ResponseGoldDetailList.class);
                    if(responseSignList!=null){
                        if ("0".equals(responseSignList.getStatus())) {
                            //这里我们要做的一个操作就是更新列表签到的列表；
                            mMyAdapter.refresh(responseSignList.getRecords());
                        }else {
                            DisPlayContent(responseSignList.getMessage());
                        }
                    }else{
                        DisPlay("网络异常");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络异常");
                }

            }
        }, pair);

    }

    @Override
    public void initView() {
        mRvGoldDetail.setLayoutManager(new LinearLayoutManager(getActivity()));
        mMyAdapter = new MyAdapter(null);
        mRvGoldDetail.setAdapter(mMyAdapter);
    }


    @Override
    public void onRefresh() {
        requestGoldDetail();
    }

    public class MyAdapter extends BaseRecyclerViewAdapter<ResponseGoldDetailList.GoldDetail>{

        public MyAdapter(List<ResponseGoldDetailList.GoldDetail> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_gold_detail;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, ResponseGoldDetailList.GoldDetail bean, int position) {

            holder.getTextView(R.id.tv_my_gold_number).setText(bean.getBalance()+"");
            holder.getTextView(R.id.tv_source).setText(bean.getTitle());

            setGoldCoinData(holder.getTextView(R.id.tv_add_gold_number),bean.getMoney());
            holder.getTextView(R.id.tv_date).setText(bean.getAddtime());
        }

        private void setGoldCoinData(TextView textView, String money) {
            int parseInt=0;
            try {
                parseInt=Integer.parseInt(money);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            //设置金额；
            if(parseInt<0){
                textView.setText(money);
                textView.setTextColor(Color.RED);
            }else if(parseInt == 0){
                textView.setText(money);
                textView.setTextColor(Color.GRAY);
            } else{
                textView.setText("+"+money);
                textView.setTextColor(textView.getContext().getResources().getColor(R.color.dark_green));
            }
        }
    }
}
